/**
 * Created by IntelliJ IDEA.
 * User: Tomek
 * Date: 06.04.13
 * Time: 17:44
 * To change this template use File | Settings | File Templates.
 */
package AS3.calculator {
import AS3.ConvertedEmission;
import AS3.CountryEmission;
import AS3.EnergyConsumption;
import AS3.constants.Coefficients;
import AS3.constants.ConvertRatio;
import AS3.constants.combo.FossilUnits;

import flash.utils.Dictionary;

public class UnitsConverterCalculator {

    private var unitsGJRatioMap:Dictionary = new Dictionary();

    public function UnitsConverterCalculator() {
        unitsGJRatioMap[FossilUnits.MWh_PER_YEAR] = ConvertRatio.MWh_PER_YEAR_RATIO_GJ;
        unitsGJRatioMap[FossilUnits.GJ_PER_YEAR] = 1;
        unitsGJRatioMap[FossilUnits.MJ_PER_YEAR] = ConvertRatio.MJ_PER_YEAR_RATIO_GJ;
        unitsGJRatioMap[FossilUnits.BTU_PER_DAY] = ConvertRatio.BTU_PER_DAY_RATIO_GJ;
        unitsGJRatioMap[FossilUnits.MLN_BTU_PER_YEAR] = ConvertRatio.MLN_BTU_PER_YEAR_RATIO_GJ;
        unitsGJRatioMap[FossilUnits.KCAL_PER_DAY] = ConvertRatio.KCAL_PER_DAY_RATIO_GJ;
        unitsGJRatioMap[FossilUnits.KWh_PER_DAY] = ConvertRatio.KWh_PER_DAY_RATIO_GJ;
    }

    public  function convertToUnits(emissions:CountryEmission,unit:int): ConvertedEmission {
        var converted:ConvertedEmission = new ConvertedEmission();



        converted.general = convertValue(unit,emissions.general,emissions.generalEnergy);
        converted.deforestation = convertValue(unit,emissions.deforestation,null);
        converted.electricity = convertValue(unit,emissions.electricity,emissions.directElectricity);
        converted.consumption = convertValue(unit,emissions.consumption,emissions.personalConsumptionEnergy);
        converted.food = convertValue(unit,emissions.food,emissions.foodEnergy);
        converted.flights = convertValue(unit,emissions.flights,emissions.airTransportEnergy);
        converted.publicTransport = convertValue(unit,emissions.publicTransport,emissions.publicTransportEnergy);
        converted.carProduction = convertValue(unit,emissions.carProduction,emissions.carProductionEnergy);
        converted.fuelBurnings = convertValue(unit,emissions.fuelBurnings,emissions.fuelBurningsEnergy);
        converted.airCondition = convertValue(unit,emissions.airCondition,emissions.airConditioningEnergy);
        converted.warmWater = convertValue(unit,emissions.warmWater,emissions.hotWaterEnergy);
        converted.heating = convertValue(unit,emissions.heating,emissions.heatingEnergy);
        converted.maintenance = convertValue(unit,emissions.maintenance,emissions.constructionMaintenanceEnergy);

        var totalEnergyConsumption:Number = calcTotalEnergyConsumption(emissions);

        if (unit == FossilUnits.KG_CO2_PER_DAY && unit == FossilUnits.KG_CO2_PER_MONTH && unit == FossilUnits.TONNE_CO2_PER_YEAR) {
            converted.averageValue = convertValue(unit,emissions.averageEmission,null);
            converted.exportValue = convertValue(unit,emissions.exportEmission,null);

            converted.emissionEU = convertValue(unit,Coefficients.EMISSION_EU,null);
            converted.emissionUS = convertValue(unit,Coefficients.EMISSION_US,null);
            converted.emissionCH = convertValue(unit,Coefficients.EMISSION_CHINA,null);
            converted.emissionIndia = convertValue(unit,Coefficients.EMISSION_INDIA,null);

            converted.totalValue = sumAllEmissions(converted);
            converted.originalValue = calcReferenceSum(emissions);
        }
        else {
            converted.averageValue = convertValue(unit,emissions.averageConsumptionGJ,null);

            //Export energy?Does it make sense?
            converted.exportValue = convertValue(unit,emissions.exportEmission,null);

            converted.emissionEU = convertValue(unit,Coefficients.CONSUMPTION_GJ_EU,null);
            converted.emissionUS = convertValue(unit,Coefficients.CONSUMPTION_GJ_US,null);
            converted.emissionCH = convertValue(unit,Coefficients.CONSUMPTION_GJ_CHINA,null);
            converted.emissionIndia = convertValue(unit,Coefficients.CONSUMPTION_GJ_INDIA,null);

            converted.totalValue = convertEnergyValue(unit, totalEnergyConsumption);
            converted.originalValue = totalEnergyConsumption;
        }

        var totalConsumptionKWhDay:Number = convertEnergyValue(FossilUnits.KWh_PER_DAY, totalEnergyConsumption);
        converted.kWhDayCoal = emissions.fossilConsumption.getCoalShare() * totalConsumptionKWhDay;
        converted.kWhDayOil = emissions.fossilConsumption.getOilShare() * totalConsumptionKWhDay;
        converted.kWhDayGas = emissions.fossilConsumption.getGasShare() * totalConsumptionKWhDay;




        /*
         converted.coalConsumption = converted.originalSumEmission *  emissions.coalTonnePerCO2Tonne * 1000;
         converted.oilConsumption = converted.originalSumEmission *  emissions.oilTonnePerCO2Tonne * 1000;
         converted.gasConsumption = converted.originalSumEmission * emissions.gasM3PerCO2Tonne / 1.25;
         */

        converted.coalConsumption = emissions.fossilConsumption.coal;
        converted.oilConsumption = emissions.fossilConsumption.oil;
        converted.gasConsumption = emissions.fossilConsumption.gas;

        return converted;
    }


    private  function sumAllEmissions(converted:ConvertedEmission):Number {
        return converted.general + converted.deforestation + converted.electricity +
                converted.consumption + converted.food + converted.flights +
                converted.publicTransport + converted.carProduction + converted.fuelBurnings +
                converted.airCondition + converted.warmWater + converted.heating +
                converted.maintenance;

    }

    private  function calcReferenceSum(original:CountryEmission):Number {
        return original.general + original.deforestation + original.electricity +
                original.consumption + original.food + original.flights +
                original.publicTransport + original.carProduction + original.fuelBurnings +
                original.airCondition + original.warmWater + original.heating +
                original.maintenance;

    }

    private function calcTotalEnergyConsumption(countryEmission:CountryEmission):Number {
        return countryEmission.personalConsumptionEnergy.overallEnergy +
                countryEmission.airTransportEnergy.overallEnergy +
                countryEmission.heatingEnergy.overallEnergy +
                countryEmission.directElectricity.overallEnergy +
                countryEmission.hotWaterEnergy.overallEnergy +
                countryEmission.constructionMaintenanceEnergy.overallEnergy +
                countryEmission.carProductionEnergy.overallEnergy +
                countryEmission.airTransportEnergy.overallEnergy +
                countryEmission.publicTransportEnergy.overallEnergy +
                countryEmission.fuelBurningsEnergy.overallEnergy +
                countryEmission.foodEnergy.overallEnergy +
                countryEmission.generalEnergy.overallEnergy;
    }


    private function convertValue(unit:int,emissions:Number,energyConsumption:EnergyConsumption):Number {
        var result:Number = new Number(emissions);

        if(unit == FossilUnits.KG_CO2_PER_DAY){
            result = ConvertRatio.TON_CO2_YEAR_PER_CO2_DAY * result;
        }
        else if(unit == FossilUnits.KG_CO2_PER_MONTH){
            result = ConvertRatio.TON_CO2_YEAR_PER_CO2_MONTH * result
        }
        else {
            if(energyConsumption == null){
                result = 0;
            }
            else {
                result =  unitsGJRatioMap[unit] * energyConsumption.overallEnergy;
            }
        }
        return result;
    }

    private function convertEnergyValue(unit:int,energyValue:Number):Number{
        return unitsGJRatioMap[unit] * energyValue;
    }

}
}
